@inherits MiniRazor.TemplateBase<CodeEntity>
@namespace OSharp.CodeGeneration.Templates
@using System;
@using System.Collections.Generic
@using System.Linq
@using OSharp.CodeGeneration.Services.Entities
@using OSharp.CodeGeneration.Utils
@using OSharp.Collections;
@using OSharp.Exceptions;
@using OSharp.Extensions;
@{
  CodeEntity entity = Model;
  string keyType = TypeHelper.ToSingleTypeName(entity.PrimaryKeyTypeFullName);
  List<string> addNamespaces = entity.Properties.Where(m => m.IsOutputDto && m.RelateEntity != null).Select(m => m.RelateEntity)
    .Union(entity.Properties.Where(m => m.IsOutputDto && m.TypeName.Contains(".") && !m.TypeName.StartsWith("System.")).Select(m => m.TypeName))
    .Select(m => TypeHelper.GetNamespace(m).Replace(".Entities", ".Dtos")).Distinct().Where(m => m != entity.Module.Namespace + ".Dtos").ToList();
    
}// -----------------------------------------------------------------------
// <auto-generated>
//    此代码由代码生成器生成。
//    手动更改此文件可能导致应用程序出现意外的行为。
//    如果重新生成代码，对此文件的任何修改都会丢失。
//    如果需要扩展此类：可遵守如下规则进行扩展：
//      1.横向扩展：如需添加额外的属性，可新建文件“@(entity.Name)OutputDto.cs”的分部类“public partial class @(entity.Name)OutputDto”添加属性
// </auto-generated>
//
// <copyright file="@(entity.Name)OutputDto.generated.cs" company="@entity.Module.Project.Company">
//    @entity.Module.Project.Copyright
// </copyright>
// <site>@entity.Module.Project.SiteUrl</site>
// <last-editor>@entity.Module.Project.Creator</last-editor>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

using OSharp.Entity;
using OSharp.Mapping;

using @(entity.Module.Namespace).Entities;
@foreach (var space in addNamespaces)
{
<text>using @(space);</text>
}

namespace @(entity.Module.Namespace).Dtos
{
    /// &lt;summary&gt;
    /// 输入DTO：@(entity.Display)信息
    /// &lt;/summary&gt;
    [MapFrom(typeof(@(entity.Name)))]
    [Description("@(entity.Display)信息")]
    public partial class @(entity.Name)OutputDto : @(GetInterfaces(entity))
    {
        /// <summary>
        /// 初始化一个<see cref="@(entity.Name)OutputDto"/>类型的新实例
        /// </summary>
        public @(entity.Name)OutputDto()
        { }

        /// <summary>
        /// 初始化一个<see cref="@(entity.Name)OutputDto"/>类型的新实例
        /// </summary>
        public @(entity.Name)OutputDto(@(entity.Name) entity)
        {
            Id = entity.Id;
            @(GetCtorString(entity, entity.Properties.Where(m => m.IsOutputDto && !m.IsNavigation).ToArray()))
        }

        /// &lt;summary&gt;
        /// 获取或设置 编号
        /// &lt;/summary&gt;
        [DisplayName("编号")]
        public @(keyType) Id { get; set; }

@foreach (CodeProperty prop in entity.Properties.Where(m => m.IsOutputDto))
{
<text>        /// &lt;summary&gt;
        /// 获取或设置 @(prop.Display)
        /// &lt;/summary&gt;
        [DisplayName("@(prop.Display)")]
        public @GetPropString(prop) @prop.Name { get; set; }

</text>
}
@if (entity.HasLocked)
{
<text>        /// <summary>
        /// 获取或设置 是否锁定
        /// </summary>
        [DisplayName("是否锁定")]
        public bool IsLocked { get; set; }

</text>
}
@if (entity.HasCreatedTime || entity.HasCreationAudited)
{
<text>        /// <summary>
        /// 获取或设置 创建时间
        /// </summary>
        [DisplayName("创建时间")]
        public DateTime CreatedTime { get; set; }

</text>
}
@if (entity.HasCreationAudited)
{
<text>        /// <summary>
        /// 获取或设置 创建者编号
        /// </summary>
        [DisplayName("创建者编号")]
        public @(keyType)? CreatorId { get; set; }

</text>
}
@if (entity.HasUpdateAudited) {
<text>        /// <summary>
        /// 获取或设置 更新者编号
        /// </summary>
        [DisplayName("更新者编号")]
        public @(keyType)? LastUpdaterId { get; set; }

        /// <summary>
        /// 获取或设置 最后更新时间
        /// </summary>
        [DisplayName("更新时间")]
        public DateTime? LastUpdatedTime { get; set; }

</text>
}
@if (entity.IsDataAuth)
{
<text>        /// <summary>
        /// 获取或设置 是否可更新的数据权限状态
        /// </summary>
        public bool Updatable { get; set; }

        /// <summary>
        /// 获取或设置 是否可删除的数据权限状态
        /// </summary>
        public bool Deletable { get; set; }

</text>
}
    }
}
@functions
{
    private static string GetInterfaces(CodeEntity entity)
    {
      List<string>list = new List<string>();
      list.Add("IOutputDto");
      list.AddIf("ILockable", entity.HasLocked);
      list.AddIf("ICreatedTime", entity.HasCreatedTime);
      list.AddIf("IDataAuthEnabled", entity.IsDataAuth);
      list.AddIf("ICreationAudited<int>", entity.HasCreationAudited);
      list.AddIf("IUpdateAudited<int>", entity.HasUpdateAudited);

      return list.ExpandAndToString(", ");
    }

    private static string GetPropString(CodeProperty prop)
    {
      List<string>list = new List<string>();
      if (!prop.IsNavigation)
      {
        string propType = TypeHelper.ToSingleTypeName(prop.TypeName);
        //可空的值类型，添加?符号
        Type tmpType = Type.GetType(prop.TypeName);
        if (prop.IsNullable && tmpType is {IsValueType: true })
        {
            propType += "?";
        }
        list.Add(propType);
      }
      else
      {
        string output = TypeHelper.ToSingleTypeName(prop.RelateEntity) + "OutputDto";
        list.Add(prop.TypeName == "ICollection<>" ? $"ICollection<{output}>" : output);
      }
      return list.ExpandAndToString("");
    }

    private static string GetCtorString(CodeEntity entity, CodeProperty[] props)
    {
      List<string>list = new List<string>();
      foreach (CodeProperty prop in props)
      {
        list.Add($"{prop.Name} = entity.{prop.Name}");
      }
      list.AddIf("IsLocked = entity.IsLocked", entity.HasLocked);
      list.AddIf("CreatedTime = entity.CreatedTime", entity.HasCreatedTime);
      list.AddIf("CreatorId = entity.CreatorId", entity.HasCreationAudited);
      list.AddIf("LastUpdaterId = entity.LastUpdaterId", entity.HasUpdateAudited);
      list.AddIf("LastUpdatedTime = entity.LastUpdatedTime", entity.HasUpdateAudited);
      string str = list.ExpandAndToString(";\r\n            ");
      if (list.Count > 0)
      {
        str += ";";
      }
      return str;
    }
}
